//-----------------------------------------------------------------------
// <copyright file="a_RateLogDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_RateLogDAOBase
    {
        #region Common methods
        public virtual a_RateLog Createa_RateLogFromReader(IDataReader reader)
        {
            a_RateLog item = new a_RateLog();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("RID"))) item.RID = (int)reader["RID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ImgID"))) item.ImgID = (int)reader["ImgID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CookieID"))) item.CookieID = (string)reader["CookieID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Rating"))) item.Rating = (int)reader["Rating"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_RateLog methods
            
        public virtual void Createa_RateLog(a_RateLog a_RateLog)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RateLog_ins");
                
                database.AddInParameter(dbCommand, "@ImgID", DbType.Int32, a_RateLog.ImgID);
                database.AddInParameter(dbCommand, "@CookieID", DbType.AnsiString, a_RateLog.CookieID);
                database.AddInParameter(dbCommand, "@Rating", DbType.Int32, a_RateLog.Rating);
                database.AddOutParameter(dbCommand, "@RID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_RateLog.RID = (int)database.GetParameterValue(dbCommand, "@RID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Createa_RateLog methods

        public virtual void Createa_RateLog(int ImgId, string Ckid, int Rating)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Rating_Add");

                database.AddInParameter(dbCommand, "@DocID", DbType.Int32, ImgId);
                database.AddInParameter(dbCommand, "@CookieID", DbType.AnsiString, Ckid);
                database.AddInParameter(dbCommand, "@Rating", DbType.Int32, Rating);

                database.ExecuteNonQuery(dbCommand);
                //a_RateLog.RID = (int)database.GetParameterValue(dbCommand, "@RID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_RateLog methods
        
        public virtual void Updatea_RateLog(a_RateLog a_RateLog)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RateLog_upd");            
                
                database.AddInParameter(dbCommand, "@RID", DbType.Int32, a_RateLog.RID);
                database.AddInParameter(dbCommand, "@ImgID", DbType.Int32, a_RateLog.ImgID);
                database.AddInParameter(dbCommand, "@CookieID", DbType.AnsiString, a_RateLog.CookieID);
                database.AddInParameter(dbCommand, "@Rating", DbType.Int32, a_RateLog.Rating);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_RateLog methods
        public virtual void Deletea_RateLog(int rid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RateLog_del");
                
                database.AddInParameter(dbCommand, "@RID", DbType.Int32, rid);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_RateLog methods
        
        public virtual a_RateLog Geta_RateLog(int rid)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RateLog_sel");
                
                database.AddInParameter(dbCommand, "@RID", DbType.Int32, rid);
                
                a_RateLog a_RateLog =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_RateLog = Createa_RateLogFromReader(reader);
                        reader.Close();
                    }
                }
                return a_RateLog;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_RateLogList methods
        public virtual a_RateLogCollection Geta_RateLogList(a_RateLogColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_RateLog_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_RateLogCollection a_RateLogCollection = new a_RateLogCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_RateLog a_RateLog = Createa_RateLogFromReader(reader);
                        a_RateLogCollection.Add(a_RateLog);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_RateLogCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_RateLogCollection Geta_RateLogList(a_RateLogColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_RateLogList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}